﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Peppers.Win32;

namespace System.Peppers.UI
{
    /// <summary>
    /// Handle to a bitmap, used in many Gdi32 calls
    /// </summary>
    public sealed class HBitmap : UnmanagedObject
    {
        /// <summary>
        /// Implicitly casts from IntPtr to HBitmap
        /// </summary>
        /// <param name="handle">Handle to use</param>
        /// <returns>Returns a new HBitmap object</returns>
        public static implicit operator HBitmap(IntPtr handle)
        {
            return new HBitmap(handle);
        }

        /// <summary>
        /// Creates a new HBitmap
        /// </summary>
        /// <param name="handle">Handle of the bitmap</param>
        public HBitmap(IntPtr handle)
        {
            Handle = handle;
        }

        #region IDisposable Members

        /// <summary>
        /// Calls Gdi32.DeleteObject
        /// </summary>
        public override void Dispose()
        {
            base.Dispose();

            if (Handle != IntPtr.Zero)
            {
                Gdi32.DeleteObject(Handle);
                Handle = IntPtr.Zero;
            }
        }

        #endregion
    }
}
